package com.duoec.dw.lineage.utils;

import com.duoec.dw.lineage.dto.TableFieldDto;
import com.duoec.dw.lineage.dto.TableInfoDto;
import com.duoec.dw.lineage.dto.TableLineageDto;

import java.util.LinkedHashSet;

/**
 * @author xuwenzhen
 */
public class TableLineageConverter {
    /**
     * 将表血缘关系转换成表基本信息
     *
     * @param tableLineage 表血缘关系
     * @return 表基本信息
     */
    public static TableInfoDto converter(TableLineageDto tableLineage) {
        TableInfoDto table = new TableInfoDto();
        table.setDatabaseName(tableLineage.getDatabaseName());
        table.setTableName(tableLineage.getTableName());
        table.setComment(tableLineage.getComment());
        LinkedHashSet<TableFieldDto> fields = new LinkedHashSet<>();
        table.setFields(fields);

        tableLineage.getFields().forEach(item -> {
            TableFieldDto field = new TableFieldDto();
            field.setIndex(item.getIndex());
            field.setDataType(item.getDataType());
            field.setColName(item.getColName());
            field.setComment(item.getComment());
            fields.add(field);
        });

        return table;
    }
}
